/**
 * @file       ConfimBox.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Jan 12, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "ConfimBox.h"
#include "IEventListener.h"
#include "KeyboardListener.h"
#include "typedefine.h"
#include "MenuButton.h"
#include "errorcode.h"
#include "EventQueue.h"

namespace NS_LCD_AFW
{

CConfimBox::CConfimBox() :
		m_pLabel(NULL), m_pOkBtn(NULL), m_pCancelBtn(NULL), m_pParentPanel(NULL)
{
	this->setHightlightable(false);
	this->setFocusable(true);

}

CConfimBox::CConfimBox(CPanel *pParent) :
		m_pLabel(NULL), m_pOkBtn(NULL), m_pCancelBtn(NULL), m_pParentPanel(
				pParent)
{
	this->setHightlightable(false);
	this->setFocusable(true);
}

CConfimBox::~CConfimBox()
{
	this->m_pCancelBtn = NULL;
	this->m_pOkBtn = NULL;
	this->m_pLabel = NULL;
	m_pParentPanel = NULL;
}

int CConfimBox::onCreate()
{
	int iRet = ERR_SUCCESS;

	this->cleanChildObject();
	OBJ_NEW(CLabel, m_pLabel);
	OBJ_NEW(CMenuButton, m_pOkBtn);
	OBJ_NEW(CMenuButton, m_pCancelBtn);
	m_pLabel->create("CConfimBox::Label_Text1", CRect(3, 1, LCD_WIDTH - 3, 5),
			this);
	m_pOkBtn->create("CConfimBox::m_OkBtn", CRect(4, 7, 6, 1), this);
	m_pOkBtn->setText("  OK  ");
	m_pCancelBtn->create("CConfimBox::m_CancelBtn", CRect(11, 7, 6, 1), this);
	m_pCancelBtn->setText("CANCEL");
	IEventListener *pOkListener = new CKeyboardListener(this,
			ENTER_KEY_PRESSED_EVENT_ID,
			(AFW_EventCallBack) (&CConfimBox::onOkBtnClick));
	m_pOkBtn->addEventListener(pOkListener);
	IEventListener *pCancelListener = new CKeyboardListener(this,
			ENTER_KEY_PRESSED_EVENT_ID,
			(AFW_EventCallBack) (&CConfimBox::onCancelBtnClick));
	m_pCancelBtn->addEventListener(pCancelListener);

	this->add(m_pLabel);
	this->add(m_pOkBtn);
	this->add(m_pCancelBtn);

	this->getPageInfo().setHightLightObjIndex(2);

	if (ERR_SUCCESS != (iRet = CPanel::onCreate()))
	{
		return iRet;
	}
	return ERR_SUCCESS;
}

void CConfimBox::onOkBtnClick(int id, int param)
{
	onOk();
	this->close(ID_OK);
}

int CConfimBox::onEscKeyPress()
{
	onCancelBtnClick(this->getObjId(), ID_CANCLE);
	return ERR_SUCCESS;
}

int CConfimBox::onEnterKeyPress()
{
	return CPanel::onEnterKeyPress();
}

void CConfimBox::onCancelBtnClick(int id, int param)
{
	onCancel();
	this->close(ID_CANCLE);
}

void CConfimBox::onOk()
{

}

void CConfimBox::onCancel()
{

}

void CConfimBox::setText(const std::string & strText)
{
	if (NULL != m_pLabel)
	{
		m_pLabel->setText(strText);
	}
}

int CConfimBox::onClose()
{
	return ERR_SUCCESS;
}

int CConfimBox::doModal(std::string info)
{
	int iRet = ERR_SUCCESS;
	if (ERR_SUCCESS
			!= (iRet = this->create("CConfimBox", DEFAULT_RECT, m_pParentPanel)))
	{
		return iRet;
	}

	this->setText(info);

	if (ERR_SUCCESS != (iRet = this->show()))
	{
		return iRet;
	}

	IEvent *pEvent = NULL;
	for (;;)
	{

		if (!AfwGetAppEventQueue()->getEvent(pEvent)
				&& !AfwGetSysEventQueue()->getEvent(pEvent))
		{
			continue;
		}

		iRet = this->handler(pEvent);

		if (IEvent::APP_EVENT_CODE == pEvent->getCode()
				&& PANEL_CLOSE_EVENT_ID == pEvent->getEventId())
		{
			if (ERR_SUCCESS == iRet)
			{
				iRet = pEvent->getParam();

				OBJ_DESTORY(pEvent);
//				if (NULL != m_pParentPanel)
//				{
//					m_pParentPanel->setVisible(true);
//					m_pParentPanel->show();
//				}
				return iRet;
			}
		}

		OBJ_DESTORY(pEvent);
	}
	return iRet;
}
}

/* namespace NS_LCD_AFW */
